(function(){
  // num: 传入点亮星星的个数
  // finalNum: 最终点亮星星的个数
  // tempNum: 一个中间值
  var num = 0,finalNum = 0, tempNum = 0;

  var my_list = document.getElementById("myStarRating");
  var list = my_list.getElementsByTagName('li');
  console.log(list);
  console.log("star-rating begin");

  function fnShow(num) {
    finalNum = num || tempNum; // 如果传入的num为0，则finalNum取tempNum值
    for (var i = 0;i < list.length;i++) {
      list[i].className = i < finalNum ? 'light' : '';
    }
  }
  for (var i = 1;i <= list.length;i++) {
    list[i - 1].index = i;

    list[i - 1].addEventListener("mouseover", function (e) {
      fnShow(this.index);
    }, false);
    list[i - 1].addEventListener("mouseout", function (e) {
      fnShow(0);
    }, false);
    // click事件会触发一次mouseover和一次mouseout，所以点击时改变tempnum确定鼠标离开时几颗星亮
    list[i - 1].addEventListener("tap", function (e) {
      tempNum = this.index;
      console.log("评分: " + e.target.innerText);
      return this.index;
    }, false);
  }
})();
